<template>
  <transition name="dialog-fade">
    <div class="os-dialog__wrapper" v-show="visible" @click.self="handleClose">
      <div
        class="os-dialog"
        :style="{ width: width + '%', marginTop: top + 'vh' }"
      >
        <div class="os-dialog__header">
          <span class="os-dialog__title">{{ title }}</span>
          <button class="os-dialog__headerbtn" @click="handleClose">
            <i class="os-icon-close" />
          </button>
        </div>
        <div class="os-dialog__body">
          <slot></slot>
        </div>
        <div class="os-dialog__footer" v-if="$slots.footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  name: "OsDialog",
  props: {
    title: {
      type: String,
      default: "对话框",
    },
    width: {
      type: String,
      default: "50",
    },
    top: {
      typeof: String,
      default: "30",
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    handleClose() {
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="scss">
.os-dialog__wrapper {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  margin: 0;
  z-index: 2001;
  background-color: rgba(0, 0, 0, 0.5);
  .os-dialog {
    position: relative;
    margin: 15vh auto 50px;
    background: #fff;
    border-radius: 10px;
    box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
    box-sizing: border-box;
    width: 600px;
    &__header {
      padding: 20px 20px 10px;
      .os-dialog__title {
        line-height: 24px;
        height: 24px;
        font-size: 18px;
        color: #303133;
      }
      .os-dialog__headerbtn {
        position: absolute;
        top: 10px;
        right: 10px;
        padding: 0;
        background: transparent;
        border: none;
        outline: none;
        cursor: pointer;
        font-size: 16px;
        .el-icon-close {
          color: #909399;
        }
      }
    }

    &__body {
      padding: 30px 20px;
      color: #606266;
      font-size: 14px;
      word-break: break-all;
    }
    &__footer {
      padding: 10px 20px 20px;
      text-align: right;
      box-sizing: border-box;
      .os-button:first-child {
        margin-right: 20px;
      }
    }
  }
}
.dialog-fade-enter-active {
  animation: fade 0.5s;
}

.dialog-fade-leave-active {
  animation: fade 0.5s reverse;
}

@keyframes fade {
  0% {
    opacity: 0;
    transform: translateY(-30px);
  }
  100% {
    opacity: 1;
    transform: translateY(0px);
  }
}
</style>
